package com.onpositive.dataminer.rcp.views;

import java.util.ArrayList;

import org.eclipse.jface.viewers.IStructuredSelection;

import com.onpositive.dataminer.core.DefaultUniverse;
import com.onpositive.knowledge.model.IThing;
import com.onpositive.knowledge.model.impl.MergingProperty;
import com.onpositive.knowledge.model.value.audits.AbstractAuditResult;
import com.onpositive.knowledge.model.values.DescribedValue;
import com.onpositive.knowledge.model.values.IHasValue;
import com.onpositive.knowledge.model.values.PropertyValuePair;
import com.onpositive.semantic.model.api.changes.IValueListener;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.ui.property.editors.structured.ListEnumeratedValueSelector;
import com.onpositive.units.ParsedDimension;
import com.onpositive.units.ParsedString;
import com.onpositive.units.ParsedValue;

public class ObjectDetailsViewPart extends SelectionView {

	public ObjectDetailsViewPart() {
		super("ui/objectDetailsView.dlf");

	}

	@Override
	protected Class<?> getTargetClass() {
		return IThing.class;
	}

	boolean inited;

	public ArrayList<PropertyValuePair> getValues(Boolean merge)
	{
		Binding binding = getBinding();
		if (!inited) {
			binding.addValueListener(new IValueListener<Object>() {

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChanged(Object oldValue, Object newValue) {
					getBinding("view.Values").refresh();
				}
			});
			inited=true;
		}
		Object thing = binding.getValue();
		if (thing instanceof IThing) {
			Iterable<IProperty> properties = DefaultUniverse.getInstance()
					.getProperties(thing);
			ArrayList<PropertyValuePair> rs = new ArrayList<PropertyValuePair>();
			if(merge){
				try{
					AbstractAuditResult auditResult = DefaultUniverse.getInstance().getAuditResult();
					for (IProperty p : properties) {

						Object comment = "" ;						
						Object oldValue = p.getValue(thing);
						if(oldValue instanceof IHasValue){
							Object cObj = ((IHasValue)oldValue).getComment();
							if(cObj!=null){
								comment=cObj;
							}
						}
						
						MergingProperty mProp = new MergingProperty(p, auditResult);
						Object valueObject = mProp.getValue(thing);
						if(valueObject instanceof ParsedValue) {
							ParsedValue converted = ParsedDimension.convertDimensions((ParsedValue) valueObject);
							if(converted!=null)
								valueObject = converted;
						}
						if(valueObject instanceof String){
							valueObject = new ParsedString((String) valueObject, "");
						}
						IHasValue value = valueObject!=null ? (IHasValue) valueObject : null;
						rs.add(new PropertyValuePair(p, value,thing, comment));
					}
				}
				catch(Throwable e){
					e.printStackTrace();
				}
			}
			else{			
				for (IProperty p : properties) {
					Object value2 = p.getValue(thing);
					DescribedValue dVal = (DescribedValue) value2;
					Object comment = dVal != null ? dVal.getComment() : "";
					rs.add(new PropertyValuePair(p, dVal,thing,comment));
				}
			}
			return rs;
		}
		return null;
	}
	
	public void contributeStandard()
	{
		PropertyValuePair pair = getPair();
		if( pair != null )
			pair.contributeStandard() ;		
	}
	public void contributeStandardComplete()
	{
		PropertyValuePair pair = getPair();
		if( pair != null )
			pair.contributeStandardComplete() ;	
	}

	public PropertyValuePair getPair() {
		IStructuredSelection sel = ((ListEnumeratedValueSelector<?>)getElement("t")).getSelection() ;
		if( sel == null || sel.size() == 0 )
			return null;
		
		Object obj = sel.getFirstElement() ;
		if( !(obj instanceof PropertyValuePair) )
			return null;
		
		return (PropertyValuePair) obj ;
	}
	
	public void resolveWikipediaRefs(){
		DefaultUniverse.getInstance().resolveWikipediaRefs() ;
	}
	public void saveUniverse(){
		DefaultUniverse.getInstance().save() ;
	}
	
	public void depricate(){
		
	}
}
